<?php

/*
 * 住户管理
 */

namespace App\Http\Controllers\Tenement;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Contracts\Support\JsonableInterface;


class ResidentController extends Controller{

    public function Sum($id){
        $data         = DB::table('myresident')
            ->where('user_resident',$id)
            ->count('user_resident');
        return $data;
    }


    public function AllResident(Request $request){
        if($request ->isMethod('post')){
            $village_id             = $request ->input('village_id');
            $data                   = DB::table('village')
                ->leftjoin('user','village.village_id','=','user.user_village')
                ->leftjoin('myresident','user.user_id','=','myresident.user_resident')
                ->where('user_village',$village_id)
                ->where('user_roomname','!=','')
                ->get();
            foreach ($data as $vale){
                $user_id      = $vale ->user_id;
                //$id           = $vale ->village_id;
                $identity     = $vale ->identity;
                $user_roomname  = $vale ->user_roomname;
                $floor        = $vale ->floor;
                $number       = $vale ->number;
                $room         = $vale ->room;
                $tmpArray[$user_id]['user_id']           = $user_id;
                $tmpArray[$user_id]['address']           = $user_roomname/*$floor.'-'.$number.'-'.$room*/;
                $tmpArray[$user_id]['sum']           = $identity/*$this->Sum($user_id)*/;
                $da           = array_values($tmpArray);
            }
            if ($data == true) {
                return json::json('success', '获取住房数据成功', $da);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', []);
            }

        }
    }

    //搜索信息
    public function SubmitResident(Request $request){
        if($request ->isMethod('post')){
            $value         = $request ->input('value');
            $data                   = DB::table('village')
                ->leftjoin('user','village.village_id','=','user.user_village')
                ->leftjoin('myresident','user.user_id','=','myresident.user_resident')
                ->where('floor', 'like','%'.$value.'%')
                ->orwhere('number', 'like','%'.$value.'%')
                ->orwhere('room', 'like','%'.$value.'%')
                ->get();
            foreach ($data as $vale){
                $user_id      = $vale ->user_id;
                //$id           = $vale ->village_id;
                $floor        = $vale ->floor;
                $number       = $vale ->number;
                $room         = $vale ->room;
                $user_roomname  = $vale ->user_roomname;
                $tmpArray[$user_id]['user_id']           = $user_id;
                $tmpArray[$user_id]['address']           = $user_roomname/*$floor.'-'.$number.'-'.$room*/;
                $tmpArray[$user_id]['sum']           = $this->Sum($user_id);
                $da           = array_values($tmpArray);
            }
            if ($data == true) {
                return json::json('success', '搜索内容获取成功', $da);
            } else {
                return json::json('failure', '没有搜索到你要的信息', []);
            }
        }
    }

    //显示住户
    public function SelectResident(Request $request){
        if($request ->isMethod('post')){
            $user_id           = $request ->input('user_id');
            $data              = DB::table('myresident')
                               ->select('resident_id','resident_name','resident_phone')
                               ->where('user_resident',$user_id)
                               ->get();
            if ($data == true) {
                return json::json('success', '获取住房数据成功', $data);
            } else {
                return json::json('failure', '由于网络或者不正当原因失败', []);
            }
        }
    }

    public function InfoResident(Request $request){
        if($request ->isMethod('post')){
            $resident_id    = $request ->input('resident_id');
            $data           = DB::table('myresident')
                            ->leftjoin('user','myresident.user_resident','=','user.user_id')
                            ->where('resident_id',$resident_id)
                            ->get();
            foreach ($data as $value){
                $da      = array(
                   'address'  =>$value->user_roomname/*$value->floor.'-'.$value->number.'-'.$value->room*/,
                    'name'    => $value ->resident_name,
                    'phone'   => $value ->resident_phone,
                    'time'    => $value ->resident_create_time
                );
            }
            if ($data == true) {
                return json::json('success', '获取详情数据成功', $da);
            } else {
                $arr  = (object)array();
                return json::json('failure', '由于网络或者不正当原因失败', $arr);
            }
        }

    }





}


